<?php

class updateProfileDocumentAction extends baseTLUPimAction
{

    public function execute($request)
    {

        $loggedInEmpNum = $this->getUser()->getEmployeeNumber();
        $loggedInUserName = $_SESSION['fname'];

        $this->form = new ProfileDocumentForm(array(),
            array('loggedInUser' => $loggedInEmpNum,
                'loggedInUserName' => $loggedInUserName), true);

        if ($this->getRequest()->isMethod('post')) {

            $attachId = $request->getParameter('seqNO');
            $screen = $request->getParameter('screen');

            $permission = $this->getDataGroupPermissions($screen . '_attachment', $request->getParameter('EmpID'));

            if ((empty($attachId) && $permission->canCreate()) || $permission->canUpdate()) {

                // Handle the form submission
                $this->form->bind($request->getPostParameters(), $request->getFiles());

                if ($this->form->isValid()) {

                    $empNumber = $this->form->getValue('EmpID');
                    if (!$this->IsActionAccessible($empNumber)) {
                        $this->forward(sfConfig::get('sf_secure_module'), sfConfig::get('sf_secure_action'));
                    }

                    $this->form->save();
                    $this->getUser()->setFlash('listAttachmentPane.success', __(TopLevelMessages::SAVE_SUCCESS));
                } else {

                    $validationMsg = '';
                    foreach ($this->form->getWidgetSchema()->getPositions() as $widgetName) {
                        if ($this->form[$widgetName]->hasError()) {
//                            $validationMsg .= __(TopLevelMessages::FILE_SIZE_SAVE_FAILURE) . " @ " . $widgetName;
                            $validationMsg .= __(TopLevelMessages::FILE_SIZE_SAVE_FAILURE);
                        }
                    }

                    $this->getUser()->setFlash('saveAttachmentPane.warning', $validationMsg);
                    $this->getUser()->setFlash('attachmentComments', $request->getParameter('txtAttDesc'));
                    $this->getUser()->setFlash('attachmentProfileDocumentTypeId', $request->getParameter('ProfileDocumentTypeList'));
                    $this->getUser()->setFlash('attachmentSeqNo', $request->getParameter('seqNO'));
                }
            }
        }

        $this->redirect($this->getRequest()->getReferer() . '#attachments');
    }

}
